#ifndef RENDERVIEWTHREAD_H
#define RENDERVIEWTHREAD_H

#include <QThread>
#include <QSize>
#include <QPoint>
#include <QVector3D>
#include <QColor>
#include <QMap>
#include "Cube.h"
#include "GeodesicSphere.h"
#include <GL/glew.h>
//#include <GL/gl.h>
//#include <GL/glu.h>
//#include <GL/glext.h>
#include <Camera.h>

class QGLWidget;
class QGLShader;
class QGLShaderProgram;
class RenderViewThread : public QThread
{
    Q_OBJECT
  public:
    explicit RenderViewThread(QGLWidget *view,
                              unsigned int frameRateInHz,
                              bool is2D = false,
                              const QColor& clearColor = QColor(255,255,255),
                              QObject *parent = 0);
    virtual ~RenderViewThread();
    void resize(const QSize& size);
    virtual void run();
    void setCamera(const QVector3D& eye,
                   const QVector3D& center,
                   const QVector3D& up);
    void stop();
    void setClearColor(const QColor& clearColor);

    Camera& camera();

  signals:
    void fps(unsigned int);

  public slots:


  //protected methods
  protected:
    void initializeGL();
    void beginGLFrame();
    void endGLFrame();
    virtual void update();
    virtual void render();

    // OpenGL helper functions
    static void begin2D(unsigned int w, unsigned int h);
    static void end2D();
    static void loadGLTexture(const QString& filename, GLuint &tex);

  //protected members
  protected:
    Camera mCamera;
    QGLWidget *mView;
    QColor mClearColor;
    unsigned int mFrameRateInHz;
    int mWidth, mHeight;
    QPoint mMousePosition;
    bool mIs2D, mDoResize, mDoRender;
    float mFrameSleepTime;
    QVector3D mLightPos;
    int mAngle, mAngleAccel;
    QGLShader *mVertShader, *mFragShader;
    QGLShaderProgram *mShader;
    Cube mCube;
    GeodesicSphere mSphere, mSun, mS;
};

#endif // RENDERVIEWTHREAD_H
